package com.xxx.mall.api.admin;

import com.xxx.mall.dto.PageQuery;
import com.xxx.mall.dto.SpuDTO;
import com.xxx.mall.dto.SpuPageQuery;
import com.xxx.mall.group.Add;
import com.xxx.mall.group.Update;
import com.xxx.mall.response.R;
import com.xxx.mall.service.SpuService;
import com.xxx.mall.vo.PageVO;
import com.xxx.mall.vo.SpuVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author 邢晨旭
 * {@code @date} 2023/11/24
 */
@RestController("adminSpuApi")
@RequestMapping("/admin/spu")
public class SpuApi {

    @Resource
    SpuService spuService;

    @GetMapping("/page")
    public R<PageVO<SpuVO>> page(@Valid PageQuery pageQuery, SpuPageQuery spuPageQuery) {
        return R.ok(spuService.pageList(pageQuery, spuPageQuery));
    }

    @PostMapping
    public R<Void> addSpu(@RequestBody @Validated(Add.class) SpuDTO spuDTO) {
        spuService.addSpu(spuDTO);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R<SpuVO> getById(@PathVariable Long id) {
        return R.ok(spuService.queryBySpuId(id));
    }

    @PutMapping
    public R<Void> update(@RequestBody @Validated(Update.class) SpuDTO spuDTO) {
        spuService.updateSpuInfo(spuDTO);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    public R<Void> del(@PathVariable Long id) {
        spuService.deleteBySpuId(id);
        return R.ok();
    }


    @PutMapping("/status")
    public R<Void> changeStatus(@RequestParam Long id, @RequestParam Integer status) {
        spuService.changeStatus(id, status);
        return R.ok();
    }

}
